<script lang="ts" setup>
import { onMounted, reactive, ref } from "vue";
import CryptoJS from "crypto-js";
import { ElMessage } from "element-plus";
import "element-plus/theme-chalk/el-message.css";
import router from "@/router/index";
import { getValidateCodeAPI } from "@/apis/loginApi";
import { useUserStore } from "@/stores/user";
const userStore = useUserStore();
import { useFunctionTreeStore } from "@/stores/functionTree";
import FaceCapture from '@/views/Login/faceCapture.vue'
const functionTreeStore = useFunctionTreeStore()

const loginMethod = ref('cp')


// 表单数据对象
const userInfo = ref({
  account: "",
  password: "",
  validateCode: "",
  agree: true,
  saveap: false,
});
// 加密密钥
const encryptionKey = "dfb";
//验证码信息接收对象
const validate_code_img = reactive({
  code: "",
  img: "",
});
//表单DOM对象
const formRef = ref(null);
//规则数据对象
const rules = {
  account: [{ required: true, message: "用户名不能为空" }],
  password: [
    { required: true, message: "密码不能为空" },
    { min: 6, max: 24, message: "密码长度要求6-14个字符" },
  ],
  validateCode: [{ required: true, message: "验证码不能为空" }],
  agree: [
    {
      validator: (rule, val, callback) => {
        return val ? callback() : new Error("请先同意协议");
      },
    },
  ],
};
//图形验证
const initValidateCode = async () => {
  const res1 = await getValidateCodeAPI();
  validate_code_img.code = res1.data.vcPro;
  validate_code_img.img = res1.data.vcImg;
  validate_code_img.img = `data:image/jpeg;base64, ${validate_code_img.img}`;
};
const login = ref(false);
//登录
const doLogin = () => {
  const { account, password, validateCode } = userInfo.value;
  // 调用实例方法
  formRef.value.validate(async (valid) => {
    //valid
    if (valid) {
      login.value = true;
      // 校验验证码
      if (validateCode !== validate_code_img.code) {
        ElMessage({ type: "error", message: "验证码错误" });
        login.value = false;
        await initValidateCode();
        return;
      }
      //登录接口
      const res = await userStore.getUserInfo({ account, password })
      if (res.code !== 0) {
        await initValidateCode()
        login.value = false
      }
      if (res.code === 0) {
        //记住密码
        if (userInfo.value.saveap) {
          localStorage.setItem("account", userInfo.value.account)
          localStorage.setItem(
            "password",
            CryptoJS.AES.encrypt(
              userInfo.value.password,
              encryptionKey
            ).toString()
          )
        }
        //预先加载功能树
        if (userStore.userInfo) {
          await functionTreeStore.getFunctionTreeInfo();
        }

        //   // 1. 提示用户
        ElMessage({ type: "success", message: "登录成功" });
        // 2. 跳转首页
        router.replace({ path: "/index", query: { f2ListId: "0" } })
      }
    }
  });
};
const loadcp = () => {
  if (localStorage.getItem("account")) {
    userInfo.value.account = localStorage.getItem("account")
  }

  userInfo.value.password = CryptoJS.AES.decrypt(
    localStorage.getItem("password"),
    encryptionKey
  ).toString(CryptoJS.enc.Utf8);
};
onMounted(async () => {
  await initValidateCode();
  loadcp()
});
</script>
<template>
  <div class="content-item">


    <header class="login-header">
      <div class="container m-top-20">
        <h1 class="logo">
          <RouterLink to="/login">DFB</RouterLink>
        </h1>
      </div>
    </header>


    <div class="login-section">
      <div class="wrapper">
        <nav>
          <a href="javascript:;" >管理员登录</a>
        </nav>
        <div class="account-box">
          <el-tabs style="display: flex;justify-content: center;margin-top: -20px;" v-model="loginMethod" class="demo-tabs">
            <el-tab-pane label="账密登录" name="cp"> </el-tab-pane>
            <el-tab-pane label="人脸登录" name="face"> </el-tab-pane>
          </el-tabs>

          <div class="form" v-if= "loginMethod=='cp'">
            <el-form ref="formRef" :model="userInfo" :rules="rules" status-icon label-position="right" label-width="60px">
              <el-form-item prop="account" label="账号">
                <el-input v-model="userInfo.account"></el-input>
              </el-form-item>
              <el-form-item prop="password" label="密码">
                <el-input v-model="userInfo.password" show-password></el-input>
              </el-form-item>
              <el-form-item prop="validateCode" label="验证">
                <el-row>
                  <el-col :span="12">
                    <el-input v-model="userInfo.validateCode"></el-input>
                  </el-col>
                  <el-col :span="12">
                    <el-link :underline="false"><img @click="initValidateCode()" :src="validate_code_img.img"
                        alt="验证码" /></el-link>
                  </el-col>
                </el-row>
              </el-form-item>
              <el-form-item style="margin-top: -20px;" prop="saveap" >
                <el-checkbox v-model="userInfo.saveap" size="large">
                  记住密码
                </el-checkbox>
              </el-form-item>
              <el-form-item prop="agree" >
                <el-checkbox style="margin-top: -20px;;" v-model="userInfo.agree" size="large">
                  我已同意DFB隐私条款和服务条款
                </el-checkbox>
              </el-form-item>
              <el-row justify="center">
                <el-button v-if="!login" size="large" class="subBtn" @click="doLogin">
                  <label>点击登录</label>
                </el-button>

                <el-button v-else size="large" class="subBtn" @click="doLogin" loading>
                  <label>登录中···</label>
                </el-button>


              </el-row>
            </el-form>
          </div>
          <div class="facelogin" v-if= "loginMethod=='face'">
            <FaceCapture></FaceCapture>
          </div>
        </div>
      </div>
    </div>

    <footer class="login-footer">
      <div class="container">
        <p>
          <a href="javascript:;">关于我们</a>
          <a href="javascript:;">帮助中心</a>
          <a href="javascript:;">售后服务</a>
          <a href="javascript:;">商务合作</a>
          <a href="javascript:;">搜索推荐</a>
          <a href="javascript:;">友情链接</a>
        </p>
        <p>CopyRight &copy; DFB</p>
      </div>
    </footer>
  </div>
</template>

<style scoped lang="scss">
.facelogin {
display: flex;
justify-content: center;
}

.content-item {
  min-width: 100vw;
  min-height: 100vh;
  background: white;
}

.el-button .custom-loading .circular {
  margin-right: 6px;
  width: 18px;
  height: 18px;
  animation: loading-rotate 2s linear infinite;
}

.el-button .custom-loading .circular .path {
  animation: loading-dash 1.5s ease-in-out infinite;
  stroke-dasharray: 90, 150;
  stroke-dashoffset: 0;
  stroke-width: 2;
  stroke: var(--el-button-text-color);
  stroke-linecap: round;
}

.subBtn {
  margin-top: -20px;
}

.el-col {
  display: flex;
  align-items: center;
}

.el-col img {
  margin-left: 10px;
  max-height: 100%;
  width: 120px;
  height: 40px;
}

.login-header {
  width: 100vw;
  background: #fff;
  border-bottom: 1px solid #e4e4e4;

  .container {
    width: 100%;
    flex: 1;
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
  }

  .logo {
    width: 200px;

    a {
      display: block;
      height: 132px;
      width: 100%;
      text-indent: -9999px;
      background: url("@/assets/images/DFB.png") no-repeat center 18px / contain;
    }
  }

  .sub {
    flex: 1;
    font-size: 24px;
    font-weight: normal;
    margin-bottom: 38px;
    margin-left: 20px;
    color: #666;
  }

  .entry {
    width: 120px;
    margin-bottom: 38px;
    font-size: 16px;

    i {
      font-size: 14px;
      color: $xtxColor;
      letter-spacing: -5px;
    }
  }
}

.login-section {
  background: url("@/assets/images/login-bg.png") no-repeat center / cover;
  height: 488px;
  width: 100vw;
  flex: 1;
  border-radius: 0%;

  .wrapper {
    margin: auto;
    width: 370px;
    height: 400px;
    background: rgba(243, 240, 240, 0.15);
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.15);
    border-radius: 5%;
    position: relative;
    top: 6%;

    nav {
      font-size: 14px;
      height: 55px;
      margin-bottom: 20px;
      border-bottom: 1px solid #f5f5f5;
      display: flex;
      padding: 0 40px;
      text-align: right;
      align-items: center;

      a {
        flex: 1;
        line-height: 1;
        display: inline-block;
        font-size: 18px;
        position: relative;
        text-align: center;
      }
    }
  }
}

.login-footer {
  padding: 30px 0 50px;
  background: #fff;

  p {
    text-align: center;
    color: #999;
    padding-top: 20px;

    a {
      line-height: 1;
      padding: 0 10px;
      color: #999;
      display: inline-block;

      ~a {
        border-left: 1px solid #ccc;
      }
    }
  }
}

.account-box {
  .form {
    padding: 0 20px 20px 20px;

    &-item {
      margin-bottom: 28px;

      .input {
        position: relative;
        height: 36px;

        >i {
          width: 34px;
          height: 34px;
          background: #cfcdcd;
          color: #fff;
          position: absolute;
          left: 1px;
          top: 1px;
          text-align: center;
          line-height: 34px;
          font-size: 18px;
        }

        input {
          padding-left: 44px;
          border: 1px solid #cfcdcd;
          height: 36px;
          line-height: 36px;
          width: 100%;

        }

        .code {
          position: absolute;
          right: 1px;
          top: 1px;
          text-align: center;
          line-height: 34px;
          font-size: 14px;
          background: #f5f5f5;
          color: #666;
          width: 90px;
          height: 34px;
          cursor: pointer;
        }
      }

      >.error {
        position: absolute;
        font-size: 12px;
        line-height: 28px;
        color: $priceColor;

        i {
          font-size: 14px;
          margin-right: 2px;
        }
      }
    }

    .agree {
      a {
        color: #069;
      }
    }

    .btn {
      display: block;
      width: 100%;
      height: 40px;
      color: #fff;
      text-align: center;
      line-height: 40px;
      background: $xtxColor;

      &.disabled {
        background: #cfcdcd;
      }
    }
  }

  .action {
    padding: 20px 40px;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .url {
      a {
        color: #999;
        margin-left: 10px;
      }
    }
  }
}

.subBtn {
  background: $xtxColor;
  width: 100%;
  color: #fff;
}

::v-deep .el-tabs__item {
  color:black;
  
}

::v-deep .el-tabs__item.is-active {
  color:whitesmoke;
  opacity: 1;
}

::v-deep .el-tabs__item:hover {
  color: whitesmoke;
  cursor: pointer;
  opacity: 1;
}
::v-deep .el-tabs__nav-wrap::after {
  position: static !important;
  /* background-color: #fff; */
}
::v-deep .el-tabs__active-bar {
  background-color: whitesmoke;
}
</style>
